package com.example.vv.fragmentWork.mvp;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import com.example.vv.fragmentWork.base.BaseView;
import com.example.vv.fragmentWork.base.BaseActivity;
import com.example.vv.fragmentWork.utlis.LogUtils;

import androidx.annotation.Nullable;
import butterknife.ButterKnife;

public abstract class MvpActivity<V extends BaseView, P extends MvpPresenter> extends BaseActivity {
    protected P presenter;

    public abstract int initLayout();

    public abstract P initPresenter();

    public abstract void initView();

    public abstract void initData();

    public abstract void doBusiness();

    public abstract View titleView();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setBarColor(titleView());
        presenter = initPresenter();
        presenter.attach((V) this);
        setContentView(initLayout());
        ButterKnife.bind(this);
        initView();
        initData();
        doBusiness();
        initReceiver();
    }

    private void initReceiver() {
        IntentFilter timeFilter = new IntentFilter();
        timeFilter.addAction("android.net.ethernet.ETHERNET_STATE_CHANGED");
        timeFilter.addAction("android.net.ethernet.STATE_CHANGE");
        timeFilter.addAction("android.net.conn.CONNECTIVITY_CHANGE");
        timeFilter.addAction("android.net.wifi.WIFI_STATE_CHANGED");
        timeFilter.addAction("android.net.wifi.STATE_CHANGE");
        timeFilter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
        registerReceiver(netReceiver, timeFilter);
    }

    public BroadcastReceiver netReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (action.equals(ConnectivityManager.CONNECTIVITY_ACTION)) {
                ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
                NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
                if (networkInfo != null && networkInfo.isAvailable()) {
                    int type2 = networkInfo.getType();
                    String typeName = networkInfo.getTypeName();
//                    Toast.makeText(context, typeName, Toast.LENGTH_SHORT).show();
                    switch (type2) {
                        case 0://移动 网络    2G 3G 4G 都是一样的 实测 mix2s 联通卡
                            LogUtils.Log("移动");
                            break;
                        case 1: //wifi网络
                            LogUtils.Log("wifi");
                            break;

                        case 9:  //网线连接
                            LogUtils.Log("网线");
                            break;
                    }
                } else {
                    Toast.makeText(context, "网络连接不可用", Toast.LENGTH_SHORT).show();
                }
            }
        }
    };

    @Override
    protected void onDestroy() {
        super.onDestroy();
        presenter.detach();
        unregisterReceiver(netReceiver);
    }
}
